/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-07 20:09:53
 */
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import helmet from 'helmet';
import { ValidationPipe } from './common/admin.pipe';
import { TransformInterceptor } from './common/transform.interceptor';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { HttpExceptionFilter } from './common/http-exception.filter';
import * as express from 'express';
import { ConfigService } from '@nestjs/config';
import { ConfigurationType } from '../config/configuration';
import { Logger } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { HomeModule } from './apiModule/home';
import { AdminModule } from './apiModule/admin';
import { LoggerMiddleware } from './common/log4js/logger.middleware';
import { OperationLogInterceptor } from './modules/admin/operation-record/operation-log.interceptor';
import { OperationRecordService } from './modules/admin/operation-record/operation-record.service';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);
  app.useStaticAssets('public', { prefix: '/public' });
  app.use(express.json());
  app.use(express.urlencoded({ extended: true }));
  //   全局过滤器
  app.useGlobalFilters(new HttpExceptionFilter());
  // 参数校验
  app.useGlobalPipes(new ValidationPipe());
  // 拦截器转换格式
  app.useGlobalInterceptors(new TransformInterceptor());
  // 全局注册拦截器
  app.useGlobalInterceptors(new OperationLogInterceptor(app.get(Reflector), app.get(OperationRecordService)));

  //cors：跨域资源共享，形式一：容许跨站拜访
  app.enableCors();
  //避免跨站脚本攻打
  app.use(helmet());
  const logger: Logger = new Logger('main.ts');
  app.use(new LoggerMiddleware().use);
  const config = new DocumentBuilder()
    .addBearerAuth()
    .setTitle('视频管理接口')
    .setDescription('接口文档')
    .setVersion('1.0')
    .addTag('后台管理用户', 'admin-user')
    .addTag('用户', 'user')
    .addTag('图片上传', 'upload').addTag('分类', 'category')
    .addTag('权限', 'role').addTag('系统设置', 'seting')
    .addTag('菜单', 'menu').addTag('订单', 'order')
    .addTag('标签管理', 'tag')
    .addTag('类型管理', 'category')
    .addTag('公告', 'notice')
    .addTag('电影管理', 'movie')
    .addTag('电影剧集管理', 'movies-episode')
    .addTag('轮播图', 'banner')
    .addTag('操作记录', 'operation-record')
    .build();
  const homeConfig = new DocumentBuilder()
    .addBearerAuth()
    .setTitle('前台接口')
    .setDescription('前台接口文档')
    .setVersion('1.1')
    .addTag('电影', 'movie')
    .addTag('电影剧情', 'movies-episode')
    .addTag('轮播图', 'banner')
    .addTag('标签', 'tag')
    .addTag('分类', 'category')
    .build();
  /** 后台接口文档 */
  const document = SwaggerModule.createDocument(app, config, {
    include: AdminModule,
  });
  SwaggerModule.setup('swagger', app, document);
  /** 前台接口文档 */
  const homeDocument = SwaggerModule.createDocument(app, homeConfig, {
    include: HomeModule,
  });
  SwaggerModule.setup('home-swagger', app, homeDocument);
  const configService = app.get(ConfigService<ConfigurationType>);
  const PORT = configService.get('port');
  const PREFIX = `/${configService.get('prefix')}`;
  // 门路前缀：如：http://www.test.com/api/v1/user
  app.setGlobalPrefix(PREFIX);
  await app.listen(PORT);
  console.log('http://localhost:3003/swagger');
  console.log('http://localhost:3003/home-swagger');
}
bootstrap();
